package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.qiantu.api.BuildConfig;
import com.qiantu.api.entity.LinkageBean;
import com.qiantu.api.utils.ThemeUtil;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.glide.GlideApp;

import java.util.List;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2019/09/22
 * desc   : 状态数据列表
 */
public final class LinkageTaskIconAdapter extends AppAdapter<String> {

    private static final int TYPE_NORMAL = 1;
    private static final int TYPE_MORE = 2;

    public LinkageTaskIconAdapter(@NonNull Context context) {
        super(context);
    }

    @NonNull
    @Override
    public AppAdapter<?>.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_MORE:
                return new MoreViewHolder();
            default:
                return new ViewHolder();
        }
    }

    @Override
    public void setData(@Nullable List<String> data) {
        super.setData(data);
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final ImageView linkageIcon;

        private ViewHolder() {
            super(R.layout.item_linkage_icon);
            linkageIcon = findViewById(R.id.linkage_icon);
        }

        @Override
        public void onBindView(int position) {
            String linkageTask = getItem(position);
            String url = BuildConfig.APP_RESOURCE_BASE + ThemeUtil.getIconThemeSuffix() + linkageTask + "_linkage.png";
            GlideApp.with(linkageIcon).load(url).into(linkageIcon);
            //1设备指令、2延时、3发送消息、4联动启停、5一键场景
//            switch (linkageTask.getTaskType()) {
//                case 1:
//                case 9:
//                    linkageIcon.getLayoutParams();
//                    GlideApp.with(linkageIcon).load(linkageTask.getImageUrl()).into(linkageIcon);
//                    break;
//                case 2:
//                    linkageIcon.setImageDrawable(AppApplication.getInstance().getThemeDrawable(R.attr.icon_intelligent_action_delay));
//                    break;
//                case 3:
//                    linkageIcon.setImageDrawable(AppApplication.getInstance().getThemeDrawable(R.attr.icon_intelligent_action_push));
//                    break;
//                case 4:
//                    linkageIcon.setImageDrawable(AppApplication.getInstance().getThemeDrawable(R.attr.icon_intelligent_linkage));
//                    break;
//                case 5:
//                    linkageIcon.setImageDrawable(AppApplication.getInstance().getThemeDrawable(R.attr.icon_intelligent_scenes));
//                    break;
//            }
        }
    }

    private final class MoreViewHolder extends AppAdapter<?>.ViewHolder {

        private MoreViewHolder() {
            super(new ImageView(getContext()));
            int width = getResources().getDimensionPixelSize(R.dimen.dp_10);
            int height = getResources().getDimensionPixelSize(R.dimen.dp_10);
            ImageView moreView = (ImageView) itemView;
            RecyclerView.LayoutParams rl = new RecyclerView.LayoutParams(width, height);
            rl.leftMargin = getResources().getDimensionPixelSize(R.dimen.dp_3);
            rl.topMargin = getResources().getDimensionPixelSize(R.dimen.dp_13);
            rl.topMargin = getResources().getDimensionPixelSize(R.dimen.dp_13);
            moreView.setLayoutParams(rl);
            moreView.setImageDrawable(AppApplication.getInstance().getThemeDrawable(R.attr.icon_scene_more));
            moreView.setScaleType(ImageView.ScaleType.FIT_XY);
            moreView.setAdjustViewBounds(true);
        }

        @Override
        public void onBindView(int position) {
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position < 3) {
            return TYPE_NORMAL;
        } else {
            return TYPE_MORE;
        }
    }

    @Override
    public int getItemCount() {
        if (getData() != null && getData().size() > 3) {
            return 4;
        }
        return super.getItemCount();
    }
}